//
//  SEGParkRegisterViewModel.m
//  SEGViewModel
//
//  Created by Clavis on 2020/12/25.
//

#import "SEGParkRegisterViewModel.h"
#import <SEGModel/SEGRequestManager.h>

@interface SEGParkRegisterViewModel ()

@property (nonatomic, strong) SEGRequestManager *reqManager;

@end

@implementation SEGParkRegisterViewModel

/// 刷新图形验证码
- (RACSignal *)refreshCodeImgSignal {
    if (!_refreshCodeImgSignal) {
        @weakify(self);
        _refreshCodeImgSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            [self.reqManager requestSecurityRefreshCheckCodeWithFinished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
                if (isSuccessed) {
                    if ([self.reqManager.seg_segtelcaptchasbyregistermodel.imgCode length] > 0) {
                        NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:self.reqManager.seg_segtelcaptchasbyregistermodel.imgCode options:NSDataBase64DecodingIgnoreUnknownCharacters];
                        UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
                        [subscriber sendNext:decodedImage];
                        [subscriber sendCompleted];
                    }
                } else {
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                    [subscriber sendError:error];
                }
            }];
            return nil;
        }];
    }
    return _refreshCodeImgSignal;
}


/// 发送短信验证码
- (RACCommand *)sendSMSCodeCommand {
    if (!_sendSMSCodeCommand) {
        @weakify(self);
        _sendSMSCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            RACTupleUnpack(NSString *tel, NSString *imgCodeText) = input;
            @strongify(self);
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                if (imgCodeText.length == 0) {
                    NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey:@"请填写图形验证码"}];
                    [subscriber sendError:error];
                    return nil;
                }
                
                [self.reqManager requestVerCodeForRegisterByRegister:ValueToString(tel) imgCode:imgCodeText randomToken:self.reqManager.seg_segtelcaptchasbyregistermodel.randomToken Finished:^(BOOL isSuccessed, id result, NSString* codeType, NSString* errorMessage) {
                    
                    if (!isSuccessed) {
                        NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:[codeType integerValue] userInfo:@{NSLocalizedDescriptionKey:errorMessage}];
                        [subscriber sendError:error];
                    }
                    [subscriber sendNext:self.reqManager.seg_segtelcaptchasbyregistermodel.randomToken];
                    [subscriber sendCompleted];
                }];
                return nil;
            }];
        }];
    }
    return _sendSMSCodeCommand;
}

- (SEGRequestManager *)reqManager {
    if (!_reqManager) _reqManager = [[SEGRequestManager alloc] init];
    return _reqManager;
}

@end
